package com.zj.leetcode.easy.hash;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @program: algorithm
 * @description: 最长和谐子序列
 * 594 longest-harmonious-subsequence
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class LongestHarmoniousSubsequence {
    public static void main(String[] args) {
//        int[] nums = {1, 3, 2, 2, 5, 2, 3, 7};
//        int[] nums = {1, 1, 1, 1};
//        int[] nums = {1,2,2,1};
        int[] nums = {1,2,1,3,0,0,2,2,1,3,3};
        Solution solution = new LongestHarmoniousSubsequence().new Solution();
        System.out.println(solution.findLHS(nums));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findLHS(int[] nums) {

            Arrays.sort(nums);
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }

            int max = 0;
            for (int i = 0; i < nums.length; i++) {
                int count = map.getOrDefault(nums[i] + 1, i) - i + 1;
                if(count == 1){
                    count = 0;
                }
                max = Math.max(max, count);
            }
            return max;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
